import Taro from "@tarojs/taro"
// import { pageToLogin } from "./utils.js"
import { HTTP_STATUS } from './config'
import Utils from '../utils'
import Proxy from './proxy'

const transformProdUrl = (url) => {//打包环境下做检测
  let BASE_URL = ''
    , ENV_TYPE = Taro.getEnv()
  switch (ENV_TYPE) {
    // case ENV_TYPE.WEAPP : BASE_URL = url;break;//微信小程序环境
    case Taro.ENV_TYPE.WEAPP: BASE_URL = Proxy.transmitUrlToVisit(url); break;//微信小程序环境
    case Taro.ENV_TYPE.SWAN: BASE_URL = url; break;//百度小程序环境
    case Taro.ENV_TYPE.ALIPAY: BASE_URL = url; break;//支付宝小程序环境
    case Taro.ENV_TYPE.TT: BASE_URL = url; break;//字节跳动小程序环境
    case Taro.ENV_TYPE.WEB: BASE_URL = url; break;//WEB(H5)环境
    case Taro.ENV_TYPE.RN: BASE_URL = url; break;//ReactNative 环境
    case Taro.ENV_TYPE.QUICKAPP: BASE_URL = url; break;//快应用环境
    case Taro.ENV_TYPE.QQ: BASE_URL = url; break;//QQ小程序 环境
    case Taro.ENV_TYPE.JD: BASE_URL = url; break;//京东小程序 环境
    default: BASE_URL = url; break;//默认 环境
  }

  if (Utils.getQueryVariable('env') === 'weapp') {
    BASE_URL = Proxy.transmitUrlToVisit(url)
  }

  return BASE_URL
}

const handCustomUrlofParams = (requestParams) => {
  let params = requestParams
  params.url = transformProdUrl(params.url)
  return params
}

const customInterceptor = (chain) => {
  const requestParams = handCustomUrlofParams(chain.requestParams)
  requestParams.loading ? Taro.showLoading({
    title: '加载中',
    mask:true
  }) : ''
  return chain.proceed(requestParams).then(res => {
    Taro.hideLoading()
    // 只要请求成功，不管返回什么状态码，都走这个回调
    if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
      return Promise.reject({ desc: '请求资源不存在' })

    } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
      return Promise.reject({ desc: "服务端出现了问题" })

    } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
      Taro.setStorageSync("Authorization", "")
      //   pageToLogin()
      // TODO 根据自身业务修改
      return Promise.reject({ desc: "没有权限访问" });

    } else if (res.statusCode === HTTP_STATUS.AUTHENTICATE) {
      Taro.setStorageSync("Authorization", "")
      //   pageToLogin()
      return Promise.reject({ desc: "需要鉴权" })

    } else if (res.statusCode === HTTP_STATUS.SERVER_ERROR) {
      return Promise.reject({ desc: "服务器错误" });
    } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
      return Promise.resolve(res.data)
      //   if (res.data.code === 0) {
      //     return Promise.resolve(res.data)
      //   } else if (res.data.code == '-100') {
      //     //非法登录
      //     // pageToLogin()
      //   }
      //   else {
      //     return Promise.reject(res.data)
      //   }
    }
  }).catch(error => {
    Taro.hideLoading()
    if (error.status === HTTP_STATUS.NOT_FOUND) {
      return Promise.reject({ desc: '请求资源不存在' })
    } else if (error.status === HTTP_STATUS.BAD_GATEWAY) {
      return Promise.reject({ desc: "服务端出现了问题" })

    } else if (error.status === HTTP_STATUS.FORBIDDEN) {
      Taro.setStorageSync("Authorization", "")
      // TODO 根据自身业务修改
      return Promise.reject({ desc: "没有权限访问" });

    } else if (error.status === HTTP_STATUS.AUTHENTICATE) {
      Taro.setStorageSync("Authorization", "")
      //   pageToLogin()
      return Promise.reject({ desc: "需要鉴权" })

    } else if (error.status === HTTP_STATUS.CLIENT_ERROR) {
      Taro.setStorageSync("Authorization", "")
      //   pageToLogin()
      return Promise.reject({ desc: "需要鉴权" })

    }  else if (error.status === HTTP_STATUS.SERVER_ERROR) {
      return Promise.reject({ desc: "服务器错误" });
    } else if (error.status === HTTP_STATUS.SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(error)
  })
}
// Taro 提供了两个内置拦截器
// logInterceptor - 用于打印请求的相关信息
// timeoutInterceptor - 在请求超时时抛出错误。
// const interceptors = [customInterceptor, Taro.interceptors.logInterceptor]
const interceptors = [customInterceptor]

export default interceptors